<?php
class GmIndexAction extends SzAbstractAction
{
    // 不需要验证权限的功能模块
    private $ignorePrivilege = array(
        'login', 'logout', 'getActor', 'checkLoginState',
        'updateActor', 'updateActorPassword', 'shardScript', 'shardScriptErrorHandler'
    );

    /**
     * Entrance of all GM requests.
     *
     * @param string $reqName default ''
     * @param string $reqParams default '', json format string
     * @throws SzException 20701
     * @return SzResponse $response
     */
    public function execute($reqName = '', $reqParams = '')
    {
        $response = null;
        if (!$reqName
            || !method_exists($this, $reqName)
        ) {
            $response = $this->pageIndexDisplay();
        } else {
            if ($reqName != 'login'
                && is_null(ModulePrivilege::get()->checkLoginState(false))) {
                throw new SzException(30401, null, ModulePrivilegeConst::MODULE_NAME);
            } else {
                if (!in_array($reqName, $this->ignorePrivilege)) {
                    ModulePrivilege::get()->validatePrivilege($reqName, $reqParams);
                }
                $reqParams = ($reqParams) ? json_decode($reqParams, true) : array();
                $response = call_user_func_array(array($this, $reqName), $reqParams);
            }
        }

        return $response;
    }

    /**
     * login page display
     *
     * @return SzResponse
     */
    private function pageIndexDisplay()
    {
        $appConfig = SzConfig::get()->loadAppConfig('app');

        $smarty = SzSmarty::get();
        $smarty->assign('IS_GM', true);
        $smarty->assign('IS_COMPRESS', $appConfig['API_COMPRESS']);
        $smarty->assign('WEB_HOST', $appConfig['WEB_HOST']);
        $smarty->assign('CDN_HOST', $appConfig['CDN_HOST']);
        $smarty->assign('WWW_RELATIVE_PATH', SzSystem::$WWW_RELATIVE_PATH);
        $smarty->assign('CANVAS_URL', $appConfig['CANVAS_URL']);
        $smarty->assign('JS_VER', $appConfig['JS_VER']);
        $smarty->assign('CSS_VER', $appConfig['CSS_VER']);
        $smarty->assign('PROTOCOL', SzUtility::getProtocol());
        $response = $this->buildResponse(
            $smarty->fetch('gm/index.tpl')
        );
        $response->setContentType('text/html');

        return $response;
    }

    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //-* Privilege 接口
    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    /**
     * @see ModulePrivilege::login
     */
    private function login($username, $password)
    {
        return $this->buildResponse(ModulePrivilege::get()->login($username, $password));
    }

    /**
     * @see ModulePrivilege::logout
     */
    private function logout()
    {
        return $this->buildResponse(ModulePrivilege::get()->logout());
    }

    /**
     * @see ModulePrivilege::checkLoginState
     */
    private function checkLoginState()
    {
        return $this->buildResponse(ModulePrivilege::get()->checkLoginState());
    }

    /**
     * 获取登入者数组格式的数据，剔除password数据
     *
     * @return SzResponse
     */
    private function getActor()
    {
        return $this->buildResponse(GmService::get()->getActor());
    }

    /**
     * 更新登入者信息
     *
     * @param array $data
     * @return SzResponse
     */
    private function updateActor($data)
    {
        return $this->buildResponse(ModulePrivilege::get()->updateUser(ModulePrivilege::get()->getActorId(), $data));
    }

    /**
     * 更新登入者密码
     *
     * @param string $oldPassword
     * @param string $newPassword
     * @return SzResponse
     */
    private function updateActorPassword($oldPassword, $newPassword)
    {
        return $this->buildResponse(ModulePrivilege::get()->updatePassword(ModulePrivilege::get()->getActorId(), $oldPassword, $newPassword));
    }

    /**
     * @see ModulePrivilege::getUserList
     */
    private function getUserList()
    {
        return $this->buildResponse(ModulePrivilege::get()->getUserList());
    }

    /**
     * @see ModulePrivilege::createUser
     */
    private function createUser($data)
    {
        return $this->buildResponse(ModulePrivilege::get()->createUser($data));
    }

    /**
     * @see ModulePrivilege::updateUser
     */
    private function updateUser($userId, $data)
    {
        return $this->buildResponse(ModulePrivilege::get()->updateUser($userId, $data));
    }

    /**
     * @see ModulePrivilege::changeRole
     */
    private function changeRole($userId, $roleId)
    {
        return $this->buildResponse(ModulePrivilege::get()->changeRole($userId, $roleId));
    }

    /**
     * @see ModulePrivilege::deleteUser
     */
    private function deleteUser($userId)
    {
        return $this->buildResponse(ModulePrivilege::get()->deleteUser($userId));
    }

    /**
     * @see ModulePrivilege::updatePassword
     */
    private function updatePassword($userId, $oldPassword, $newPassword)
    {
        return $this->buildResponse(ModulePrivilege::get()->updatePassword($userId, $oldPassword, $newPassword));
    }

    /**
     * @see ModulePrivilege::getRoleList
     */
    private function getRoleList()
    {
        return $this->buildResponse(ModulePrivilege::get()->getRoleList());
    }

    /**
     * @see ModulePrivilege::createRole
     */
    private function createRole($data)
    {
        return $this->buildResponse(ModulePrivilege::get()->createRole($data));
    }

    /**
     * @see ModulePrivilege::updateRole
     */
    private function updateRole($roleId, $data)
    {
        return $this->buildResponse(ModulePrivilege::get()->updateRole($roleId, $data));
    }

    /**
     * @see ModulePrivilege::deleteRole
     */
    private function deleteRole($roleId)
    {
        return $this->buildResponse(ModulePrivilege::get()->deleteRole($roleId));
    }

    /**
     * @see ModulePrivilege::changeRolePrivilege
     */
    private function changeRolePrivilege($roleId, $privilegeIds)
    {
        return $this->buildResponse(ModulePrivilege::get()->changeRolePrivilege($roleId, $privilegeIds));
    }

    /**
     * @see ModulePrivilege::getPrivilegeList
     */
    private function getPrivilegeList()
    {
        return $this->buildResponse(ModulePrivilege::get()->getPrivilegeTreeList());
    }

    /**
     * @see ModulePrivilege::createPrivilege
     */
    private function createPrivilege($data)
    {
        return $this->buildResponse(ModulePrivilege::get()->createPrivilege($data));
    }

    /**
     * @see ModulePrivilege::updatePrivilege
     */
    private function updatePrivilege($privilegeId, $data)
    {
        return $this->buildResponse(ModulePrivilege::get()->updatePrivilege($privilegeId, $data));
    }

    /**
     * @see ModulePrivilege::deletePrivilege
     */
    private function deletePrivilege($privilegeId)
    {
        return $this->buildResponse(ModulePrivilege::get()->deletePrivilege($privilegeId));
    }

    /**
     * @see ModulePrivilege::changePrivilege
     */
    private function changePrivilege($privilegeId, $pIds)
    {
        return $this->buildResponse(ModulePrivilege::get()->changePrivilege($privilegeId, $pIds));
    }

    /**
     * @see ModulePrivilege::getLoggerList
     */
    private function getLoggerList($actorId)
    {
        return $this->buildResponse(ModulePrivilege::get()->getLoggerList($actorId));
    }

    /**
     * api工具接口
     *
     * @param string $toolsName
     * <pre>
     *  1. loadApiModules()
     *  2. loadModuleApis($module)
     *  3. loadApiDetail($moduleName, $actionName)
     * </pre>
     * @param array $data
     * @return SzResponse
     */
    private function apiTools($toolsName, $data)
    {
        $response = call_user_func_array(array(GmService::get(), $toolsName), $data);
        return $this->buildResponse($response);
    }

    /**
     * 数据调整工具接口
     *
     * @param string $toolsName
     * <pre>
     *  1. loadOrms()
     *  2. loadOrmData($userId, $ormName)
     *  3. insertOrmData($ormName, $data)
     *  4. updateOrmData($ormName, $data)
     *  5. deleteOrmData($ormName, $data)
     * </pre>
     * @param array $data
     * @return SzResponse
     */
    private function ormTools($toolsName, $data)
    {
        $response = call_user_func_array(array(GmService::get(), $toolsName), $data);
        return $this->buildResponse($response);
    }

    /**
     * @see GmService::createPlayer
     */
    private function createPlayer($platformId)
    {
        return $this->buildResponse(GmService::get()->createPlayer($platformId));
    }

    /**
     * @see GmService::deletePlayer
     */
    private function deletePlayerGameData($userId)
    {
        return $this->buildResponse(GmService::get()->deletePlayer($userId));
    }

    /**
     * @see GmService::deletePlayer
     */
    private function deletePlayerCache($userId)
    {
        return $this->buildResponse(GmService::get()->deletePlayer($userId, null, true));
    }

    /**
     * @see GmService::export
     */
    private function export($userId)
    {
        $file = GmService::get()->export($userId);

        $response = $this->buildResponse('');
        $response->setFile($file);

        return $response;
    }

    /**
     * @see GmService::import
     */
    private function import($userId, $password)
    {
        return $this->buildResponse(GmService::get()->import($userId, $password));
    }

    /**
     * @see GmService::importTest
     */
    private function importTest($userId)
    {
        return $this->buildResponse(GmService::get()->importTest($userId));
    }

    /**
     * @see GmService::flushCache
     */
    private function flushCache($password)
    {
        return $this->buildResponse(GmService::get()->flushCache($password));
    }

    /**
     * @see GmService::loadPlayerData
     */
    private function loadPlayerData($userId)
    {
        /**
         * @var ModuleProfileVo $profile
         * @var ModulePaymentVo $payment
         * @var ModuleProfileInfoVoList $profileInfoList
         */
        list($profile, $payment, $profileInfoList) = GmService::get()->loadPlayerData($userId);
        return $this->buildResponse(array(
            'profile' => $profile->toarray(),
            'payment' => $payment->toarray(),
            'profileInfoList' => $profileInfoList->toarray(),
        ));
    }

    /**
     * @see GmService::editPlayerData
     */
    private function editPlayerData($userId, $data)
    {
        /**
         * @var ModuleProfileVo $profile
         * @var ModulePaymentVo $payment
         * @var ModuleProfileInfoVoList $profileInfoList
         */
        list($profile, $payment, $profileInfoList) = GmService::get()->editPlayerData($userId, $data);
        return $this->buildResponse(array(
            'profile' => $profile->toarray(),
            'payment' => $payment->toarray(),
            'profileInfoList' => $profileInfoList->toarray(),
        ));
    }

    /**
     * @see GmService::editPlayerData
     */
    private function updatePlayerExp($userId, $level)
    {
        return $this->buildResponse(GmService::get()->updatePlayerExp($userId, $level));
    }

    /**
     * @see GmService::sendItem
     */
    private function sendItem($userId, $itemDefId, $count)
    {
        return $this->buildResponse(GmService::get()->sendItem($userId, $itemDefId, $count));
    }

    /**
     * @see Gmservice::reduceItem
     */
    private function reduceItem($userId, $itemDefId, $count)
    {
        return $this->buildResponse(GmService::get()->reduceItem($userId, $itemDefId, $count));
    }

    /**
     * @see GmService::handleScript
     */
    private function handleScript($type, $password)
    {
        return $this->buildResponse(GmService::get()->handleScript($type, $password));
    }

    /**
     * @see GmService::handleServerStatus
     */
    private function handleServerStatus($handle, $msg)
    {
        return $this->buildResponse(GmService::get()->handleServerStatus($handle, $msg));
    }

    /**
     * @see GmService::handleServerStatus
     */
    private function handleMoreGameStatus($handle, $msg)
    {
        return $this->buildResponse(GmService::get()->handleMoreGameStatus($handle, $msg));
    }

    private function handleHotPlayStatus($handle, $msg) {
        return $this->buildResponse(GmService::get()->handleHotPlayStatus($handle, $msg));
    }

    private function handleGmStatus($handle, $msg) {
        return $this->buildResponse(GmService::get()->handleGmStatus($handle, $msg));
    }

    private function handleNoticeStatus($handle, $msg) {
        return $this->buildResponse(GmService::get()->handleNoticeStatus($handle, $msg));
    }

    private function handleShareStatus($handle, $msg) {
        return $this->buildResponse(GmService::get()->handleShareStatus($handle, $msg));
    }

    private function handleOperationCache($handle, $cacheKey, $uId) {
        return $this->buildResponse(GmService::get()->handleOperationCache($handle, $cacheKey, $uId));
    }

    private function checkTextScroll($is_gm=0)
    {
        return $this->buildResponse(GmService::get()->checkTextScroll($is_gm));
    }
    private function insertTextScroll($tid, $type, $start, $end, $interval,$content)
    {
        return $this->buildResponse(GmService::get()->insertTextScroll($tid, $type, $start, $end, $interval,$content));
    }
    private function deleteTextScroll($tid)
    {
        return $this->buildResponse(GmService::get()->deleteTextScroll($tid));
    }
}